﻿@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Form.cshtml";
}
@using Raffles.OA.Utility
@using Raffles.OA.Data
<form id="form" class="form-horizontal m" role="form">
    <div class="form-group">
        <label class="col-sm-3 control-label ">目标表单<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <select id="formCode" col="FormCode" class="form-control required">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">编号前缀<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="prefix" name="Prefix" col="Prefix" type="text" class="form-control required " placeholder="请输入编号前缀" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">编号规则<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <div id="codeRule" col="CodeRule">
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">流水号(位)<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="serialNumber" name="SerialNumber" col="SerialNumber" type="number" class="form-control required " placeholder="请输入流水号" max="10" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">生成规则<span class="text-red"> *</span></label>
        <div class="col-sm-9">
            <input id="generativeRule" name="GenerativeRule" col="GenerativeRule" type="text" class="form-control required" placeholder="编号生成规则" disabled />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label ">编号预览</label>
        <div class="col-sm-9">
            <span class="text-blue text-bold" id="numberPreview" style="font-size:25px"></span>
        </div>
    </div>
</form>

<script type="text/javascript">
    var id = frame.request("id");
    var serialNumberVal = "";
    var serialNumberNine = "";
    var generativeRule = "";
    var numberPreview = "";
    var codeRuleValue = '@((int)NumberRuleEnum.yyyyMMddHHmmss)';
    $(function () {
        getForm();
        $("#formCode").frameSelect({
            url: "/Common/GetDictionaryDetailByType",
            param: { dicType: "NumberRuleForm" },
            search: true
        });
        $("#codeRule").frameRadioBox({
            data: frame.getJson(@Html.Raw(typeof(NumberRuleEnum).EnumToDictionaryString())),
            default:codeRuleValue,
            onCheck: function (event) { 
                codeRuleValue = event.target.value;
                if ($('#prefix').val().length == 0 && $('#serialNumber').val().length == 0) {
                    switch (event.target.value) {
                        case '@((int)NumberRuleEnum.yyyy)':
                            setValue('@NumberRuleEnum.yyyy.GetDescription()', moment().format('YYYY'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMM)':
                            setValue('@NumberRuleEnum.yyyyMM.GetDescription()', moment().format('YYYYMM'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMdd)':
                            setValue('@NumberRuleEnum.yyyyMMdd.GetDescription()', moment().format('YYYYMMDD'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                            setValue('@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()', moment().format('YYYYMMDDHHmmss'));
                            break;
                    }
                }
                else if ($('#prefix').val().length > 0 && $('#serialNumber').val().length == 0) {
                    var prefixValue = $('#prefix').val();
                    switch (event.target.value) {
                        case '@((int)NumberRuleEnum.yyyy)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyy.GetDescription()', prefixValue + moment().format('YYYY'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMM)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMM.GetDescription()', prefixValue + moment().format('YYYYMM'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMdd)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMMdd.GetDescription()', prefixValue + moment().format('YYYYMMDD'));
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()', prefixValue + moment().format('YYYYMMDDHHmmss'));
                            break;
                    }
                }
                else if ($('#prefix').val().length == 0 && $('#serialNumber').val().length > 0) {
                    serialNumberVal = "";
                    serialNumberNine = "";
                    for (var i = 0; i < $('#serialNumber').val(); i++) {
                        if ((i + 1) == $('#serialNumber').val()) {
                            serialNumberVal += "1"
                        }
                        else {
                            serialNumberVal += "0"
                        }
                        serialNumberNine += "9"
                    }
                    generativeRule = serialNumberVal + "~" + serialNumberNine;
                    numberPreview = serialNumberVal;
                    switch (event.target.value) {
                        case '@((int)NumberRuleEnum.yyyy)':
                            setValue('@NumberRuleEnum.yyyy.GetDescription()' + generativeRule, moment().format('YYYY') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMM)':
                            setValue('@NumberRuleEnum.yyyyMM.GetDescription()' + generativeRule, moment().format('YYYYMM') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMdd)':
                            setValue('@NumberRuleEnum.yyyyMMdd.GetDescription()' + generativeRule, moment().format('YYYYMMDD') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                            setValue('@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()' + generativeRule, moment().format('YYYYMMDDHHmmss') + numberPreview);
                            break;
                    }
                }
                else {
                    var prefixValue = $('#prefix').val();
                    serialNumberVal = "";
                    serialNumberNine = "";
                    for (var i = 0; i < $('#serialNumber').val(); i++) {
                        if ((i + 1) == $('#serialNumber').val()) {
                            serialNumberVal += "1"
                        }
                        else {
                            serialNumberVal += "0"
                        }
                        serialNumberNine += "9"
                    }
                    generativeRule = serialNumberVal + "~" + serialNumberNine;
                    numberPreview = serialNumberVal;
                    switch (event.target.value) {
                        case '@((int)NumberRuleEnum.yyyy)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyy.GetDescription()' + generativeRule, prefixValue + moment().format('YYYY') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMM)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMM.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMM') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMdd)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMMdd.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMMDD') + numberPreview);
                            break;
                        case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                            setValue(prefixValue + '@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMMDDHHmmss') + numberPreview);
                            break;
                    }
                }

            }
        });
        //前缀绑定事件，流水号绑定事件
        $("#prefix,#serialNumber").on("blur", function () { 
            var prefixValue = $('#prefix').val();
            if ($("#serialNumber").val().length == 0 && $("#prefix").val().length > 0) {
                switch (codeRuleValue) {
                    case '@((int)NumberRuleEnum.yyyy)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyy.GetDescription()', prefixValue + moment().format('YYYY'));
                        break;
                    case '@((int)NumberRuleEnum.yyyyMM)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMM.GetDescription()', prefixValue + moment().format('YYYYMM'));
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMdd)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMMdd.GetDescription()', prefixValue + moment().format('YYYYMMDD'));
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()', prefixValue + moment().format('YYYYMMDDHHmmss'));
                        break;
                }
            }
            else if ($("#serialNumber").val().length > 0 && $("#prefix").val().length == 0) {
                serialNumberVal = "";
                serialNumberNine = "";
                for (var i = 0; i < $('#serialNumber').val(); i++) {
                    if ((i + 1) == $('#serialNumber').val()) {
                        serialNumberVal += "1"
                    }
                    else {
                        serialNumberVal += "0"
                    }
                    serialNumberNine += "9"
                }
                generativeRule = serialNumberVal + "~" + serialNumberNine;
                numberPreview = serialNumberVal;
                switch (codeRuleValue) {
                    case '@((int)NumberRuleEnum.yyyy)':
                        setValue('@NumberRuleEnum.yyyy.GetDescription()' + generativeRule, moment().format('YYYY') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMM)':
                        setValue('@NumberRuleEnum.yyyyMM.GetDescription()' + generativeRule, moment().format('YYYYMM') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMdd)':
                        setValue('@NumberRuleEnum.yyyyMMdd.GetDescription()' + generativeRule, moment().format('YYYYMMDD') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                        setValue('@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()' + generativeRule, moment().format('YYYYMMDDHHmmss') + numberPreview);
                        break;
                }
            }
            else {
                serialNumberVal = "";
                serialNumberNine = "";
                for (var i = 0; i < $('#serialNumber').val(); i++) {
                    if ((i + 1) == $('#serialNumber').val()) {
                        serialNumberVal += "1"
                    }
                    else {
                        serialNumberVal += "0"
                    }
                    serialNumberNine += "9"
                }
                generativeRule = serialNumberVal + "~" + serialNumberNine;
                numberPreview = serialNumberVal;
                switch (codeRuleValue) {
                    case '@((int)NumberRuleEnum.yyyy)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyy.GetDescription()' + generativeRule, prefixValue + moment().format('YYYY') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMM)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMM.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMM') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMdd)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMMdd.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMMDD') + numberPreview);
                        break;
                    case '@((int)NumberRuleEnum.yyyyMMddHHmmss)':
                        setValue(prefixValue + '@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()' + generativeRule, prefixValue + moment().format('YYYYMMDDHHmmss') + numberPreview);
                        break;
                }
            }
        });
    });

    function getForm() {
        if (!!id) {
            frame.ajax({
                url: '@Url.Content("/Manage/NumberRule/SetForm")' + '?id=' + id,
                type: 'get',
                success: function (obj) {
                    if (obj.success) {
                        $('#form').setWebControls(obj.data);   
                        codeRuleValue = frame.toString(obj.data.CodeRule)
                        $("#prefix").trigger("blur");
                    }
                }
            });
        }
        else {
            var defaultData = {};
            //默认值
            setValue('@NumberRuleEnum.yyyyMMddHHmmss.GetDescription()', moment().format('YYYYMMDDHHmmss'));
            $('#form').setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id });
            frame.ajax({
                url: '@Url.Content("/Manage/NumberRule/SaveForm")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        }
    }

    function setValue(rule, preview) {
        $("#generativeRule").val(rule);
        $("#numberPreview").text(preview);
    }
</script>